﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaBrowser.Library.Entities;
using MediaBrowser.Library.Metadata;
using System.Diagnostics;
using MediaBrowser.Library;

namespace FullRefreshService
{
    public class FullRefresh
    {
        private static DateTime startTime = DateTime.Now;

        #region Public Properties

        public static int FastMetadataChanged = 0;
        public static int SlowMetadataChanged = 0;
        public static int TotalItems = 0;
        
        public static TimeSpan ValidationTime { get; set; }
        public static TimeSpan FastMetadataTime { get; set; }
        public static TimeSpan SlowMetadataTime { get; set; }

        #endregion

        public static TimeSpan TimeAction(Action func)
        {
            var watch = new Stopwatch();
            watch.Start();
            func();
            watch.Stop();
            return (watch.Elapsed);
        }

        public static void PerformFullRefresh(Folder folder, MetadataRefreshOptions options)
        {
            folder.RefreshMetadata(options);

            ValidationTime = TimeAction(() =>
            {
                RunActionRecursively("validate", folder, item =>
                {
                    Folder f = item as Folder;
                    if (f != null) f.ValidateChildren();
                });
            });

            FastMetadataTime = TimeAction(() =>
            {
                RunActionRecursively("fast metadata", folder, item =>
                {
                    FastMetadataChanged += item.RefreshMetadata(MetadataRefreshOptions.FastOnly) ? 1 : 0;
                    TotalItems++;

                });
            });

            SlowMetadataTime = TimeAction(() =>
            {
                RunActionRecursively("slow metadata", folder, item =>
                {
                    SlowMetadataChanged += item.RefreshMetadata(MetadataRefreshOptions.Default) ? 1 : 0;
                });
            });

            Logging.Logger.Instance.WriteLine("Setting LastFullRefresh in config");
            Kernel.Instance.ConfigData.LastFullRefresh = DateTime.Now;
            Kernel.Instance.ConfigData.Save();

        }

        static string TimeSinceStart()
        {
            return new DateTime((DateTime.Now - startTime).Ticks).ToString("HH:mm:ss");
        }

        static void RunActionRecursively(string desc, Folder folder, Action<BaseItem> action)
        {
            Logging.Logger.Instance.WriteLine("Refreshing Folder: " + folder.Path);
            Logging.Logger.Instance.WriteLine(string.Format("{0} - {1} : {2} {3} (t) {4}", desc, folder.GetType(), folder.Name, folder.Path, TimeSinceStart()));
            action(folder);
            foreach (var item in folder.RecursiveChildren.OrderByDescending(i => i.DateModified))
            {
                Logging.Logger.Instance.WriteLine(string.Format("{0} - {1} : {2} {3} (t) {4}", desc, item.GetType(), item.Name, item.Path, TimeSinceStart()));
                action(item);
            }
        }
    }
}
